package com.chj.config;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

import com.chj.security.MyClientDetailService;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

	@Autowired
	AuthenticationManager authenticationManager;

	// @Autowired
	// RedisConnectionFactory redisConnectionFactory;

	@Autowired
	@Qualifier("myClientDetailsService")
	private MyClientDetailService clientDetailsService;

	// @Autowired
	// private DataSource dataSource;

	@Override
	public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
		clients.withClientDetails(clientDetailsService);
	}

	@Override
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		// endpoints.tokenStore(getRedisTokenStore());
		// endpoints.tokenStore(getJdbcTokenStore());
		endpoints.tokenStore(getInMemoryTokenStore());
		endpoints.authenticationManager(authenticationManager);
		endpoints.tokenServices(tokenServices());
	}

	@Primary
	@Bean
	public AuthorizationServerTokenServices tokenServices() {
		DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
		defaultTokenServices.setAccessTokenValiditySeconds((int) TimeUnit.MINUTES.toSeconds(10));
		defaultTokenServices.setRefreshTokenValiditySeconds((int) TimeUnit.MINUTES.toSeconds(10));
		defaultTokenServices.setSupportRefreshToken(true);
		defaultTokenServices.setReuseRefreshToken(false);
		defaultTokenServices.setTokenStore(getInMemoryTokenStore());
		return defaultTokenServices;
	}

	@Override
	public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
		// 允许表单认证
		oauthServer.allowFormAuthenticationForClients();
		oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("permitAll()");// ("isAuthenticated()");
	}

	// @Bean
	// public JdbcTokenStore getJdbcTokenStore() {
	// return new JdbcTokenStore(dataSource);
	// }

	// @Bean
	// public RedisTokenStore getRedisTokenStore() {
	// return new RedisTokenStore(redisConnectionFactory);
	// }

	@Bean
	public InMemoryTokenStore getInMemoryTokenStore() {
		return new InMemoryTokenStore();
	}
}
